#ifndef TETRIX_H
#define TETRIX_H

#define CELL_SIZE 20
#define TIMER_TIMEOUT 1000
#define RT_COL  20
#define RT_ROW 20

//config setting
#define CONFIG_FILE			"tetrix.ini"
#define CONFIG_SECTION_SERVER		"server"
#define CONFIG_SECTION_SERVER_IP	"ip"
#define CONFIG_SECTION_SERVER_PORT	"port"

#define WAIT_RESPONSE_TIMEOUT_SEC       10

#include <QMainWindow>
#include <QBasicTimer>
#include <QTimer>
#include <QKeyEvent>
#include <QColor>
#include <QTime>
#include <QPoint>
#include <stdlib.h>
#include <ace/Log_Msg.h>
#include <ace/SOCK_Stream.h>
#include <ace/SOCK_Connector.h>
#include <ace/CDR_Stream.h>
#include <ace/Thread.h>
#include <ace/Thread_Semaphore.h>

#include "russiatetrix.h"
#include "../TetrixServer/createroomreq.h"
#include "../TetrixServer/createroomresp.h"
#include "../TetrixServer/getroomlistreq.h"
#include "../TetrixServer/getroomlistresp.h"
#include "../TetrixServer/enterroomsrv.h"
#include "../TetrixServer/leaveroomreq.h"
#include "../TetrixServer/statussetreq.h"
#include "../TetrixServer/protosrvmanager.h"
#include "../tools/iniparser.h"

namespace Ui {
    class Tetrix;
}

class Tetrix : public QMainWindow {
    Q_OBJECT
public:
    Tetrix(QWidget *parent = 0);
    ~Tetrix();

    bool start();
    void gameOver();
    void restartTimer();

    void updateRoomList();

    bool doRequest(ProtoSrv &request);

    static void *worker(void *arg);

    void addRoom(const std::string &room);
    void enterRoom(const std::string &room);
    void leaveRoom(const std::string &room);
    void roomReady(const std::string &room);

    void handleNotify(std::auto_ptr<ProtoSrv> &notify);
protected:
    void changeEvent(QEvent *e);
    void paintEvent(QPaintEvent *);
    void timerEvent(QTimerEvent *);
    void keyPressEvent(QKeyEvent *);

private:
    Ui::Tetrix *ui;
    QBasicTimer _timer;

    //game setting
    //GameStatus          _status;
    quint8		_game_level;
    RussiaTetrix	_rt;

    //connection info
    QString		_ip;
    quint16		_port;
    ACE_SOCK_Stream	_conn;

    //room info
    std::string                     _cur_room;
    GetRoomListResp::RoomMap        _room_map;

    //thread sync for response
    ACE_thread_t                _worker_id;
    ACE_Thread_Semaphore        _semaphore;
    std::auto_ptr<ProtoSrv>     _response;
private slots:
    void on_action_Ready_triggered();
    void on_action_Leave_Room_triggered();
    void on_action_Enter_Room_triggered();
    void on_action_Room_List_triggered();
    void on_action_Create_Room_triggered();
    void on_actionLevel_6_triggered();
    void on_actionLevel_5_triggered();
    void on_actionLevel_4_triggered();
    void on_actionLevel_3_triggered();
    void on_actionLevel_2_triggered();
    void on_actionLevel_1_triggered();
    void on_actionExit_triggered();
    void on_actionStartGame_triggered();
};

#endif // TETRIX_H
